//
//  CustomCoreImageTextView.m
//  FFMall
//
//  Created by ale tan on 2017/10/11.
//  Copyright © 2017年 IT.Bear. All rights reserved.
//

#import "CustomCoreImageTextView.h"
#import "MessageProductCell.h"

#import "FBOrderModel.h"

@interface CustomCoreImageTextView ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UILabel *orderNumberLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tableViewToTop;

@property (nonatomic ,assign) CustomMessageType viewType;

@property (nonatomic ,strong) FBOrderModel *orderModel;
@property (nonatomic ,strong) FBProductModel *productModel;

@end

@implementation CustomCoreImageTextView

- (void)awakeFromNib{
    [super awakeFromNib];
    
    self.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickCustomView)];
    [self addGestureRecognizer:tap];
    
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

- (void)loadWithDictionary:(NSDictionary *)dic Type:(CustomMessageType)type{
    _viewType = type;
    
    switch (type) {
        //订单
        case 4:
        {
            _orderNumberLabel.hidden = NO;
            _tableViewToTop.constant = 44;
            _orderModel = [[FBOrderModel alloc]initWithDictionary:dic];
            _productModel = _orderModel.products[0];
            
            _orderNumberLabel.text = [NSString stringWithFormat:@"订单号：%@",_orderModel.orderNo];
        }
            break;
            
        //退款单
        case 6:
        {
            _orderNumberLabel.hidden = NO;
            _tableViewToTop.constant = 44;
//            _refundOrderModel = [[FFRefundOrderModel alloc]initWithDictionary:dic];
//            _productModel = _refundOrderModel.products[0];
//
//            _orderNumberLabel.text = [NSString stringWithFormat:@"退款单号：%@",_refundOrderModel.refund_id];
        }
            break;
            
        //商品
        default:
        {
            _orderNumberLabel.hidden = YES;
            _tableViewToTop.constant = 9;
            _productModel = [[FBProductModel alloc]initWithDictionary:dic];
        }
            break;
    }
    
    [_tableView reloadData];
}

#pragma mark -Events-
- (void)clickCustomView{
    switch (_viewType) {
            //订单
        case 4:
        {
            
        }
            break;
            
            //退款单
        case 6:
        {
            
        }
            break;
            
            //商品
        default:
        {
            
        }
            break;
    }
}

#pragma mark -UITableView-
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    switch (_viewType) {
        case 4:
            return _orderModel.products.count>2?2:_orderModel.products.count;
            break;
            
        case 6:
            return 0;
            break;
            
        default:
            return 1;
            break;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return ProductCellHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MessageProductCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MessageProductCell"];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"MessageProductCell" owner:nil options:nil] firstObject];;
    }
    
    switch (_viewType) {
        case 4:
            [cell loadWithModel:_orderModel.products[indexPath.row] IndexPath:indexPath];
            break;
            
//        case 6:
//            [cell loadWithModel:_refundOrderModel.products[indexPath.row] IndexPath:indexPath];
//            break;
            
        default:
            [cell loadWithModel:_productModel IndexPath:indexPath];
            cell.qtyLabel.hidden = YES;
            break;
    }
    
    return cell;
}

@end
